package com.d.main;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.Vector;
import java.util.regex.Pattern;

import org.webharvest.definition.ScraperConfiguration;
import org.webharvest.runtime.Scraper;
import org.webharvest.runtime.variables.Variable;

import com.d.entity.Account;


public class Run {

	public static void main(String[] args) {
		
		
		String rootPath = Run.class.getResource("").getPath();


		//System.out.println("folderName = "+folderName);
		

		
		Properties pro = new Properties();
		try {
			FileInputStream fisPro = new FileInputStream(new File(rootPath+"pielove.properties"));
			try {
				pro.load(fisPro);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (FileNotFoundException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		
		/*int beginId = new Integer(pro.getProperty("beginid")).intValue();
		int endId = new Integer(pro.getProperty("endid")).intValue();*/
		
		System.out.println("开始获取获取有图片帖子ID...");
		ScraperConfiguration config = null;
		try {
			File file = new File(rootPath+"pielove_smth_page.xml");
			config = new ScraperConfiguration(file);
					//"D:/eclipseWS/WebHarvest/src/pielove_smth.xml");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Scraper scraper = new Scraper(config, "output");
		int beginPage = new Integer(pro.getProperty("beginpage")).intValue();
		int endPage = new Integer(pro.getProperty("endpage")).intValue();
		Vector vecAccountWithImage = new Vector();
		for(int page = beginPage;page<=endPage;page++){
			scraper.addVariableToContext("page", page);
			scraper.setDebug(true);
			scraper.execute();
			Variable allcontent = (Variable)scraper.getContext().get("allcontent");
			if (allcontent == null || allcontent.toString().trim().length() == 0)
				continue;
			String all = allcontent.toString();
			String REGEX2 = "c\\.o\\(";
			Pattern p = Pattern.compile(REGEX2);
			String[] str = p.split(all);
			for(int i=0;i<str.length;i++){
				if(str[i].indexOf("@'")!=-1){
					String[] subStr = str[i].split(",");
					/*String REGEX2 = "c\\.o";
					Pattern p = Pattern.compile(REGEX2);
					String[] elements = */
					//String articleId = str[i].substring(1,8);
					//String authorId = str[i].substring(beginIndex, endIndex);
					Account account = new Account();
					account.setArticleId(subStr[0]);
					account.setId(subStr[2].substring(1,subStr[2].length()-1));
					
					vecAccountWithImage.add(account);
				}
				
			}
			
		}
		
		System.out.println("获取有图片帖子ID结束，开始解析每个帖子...");
		Vector vecImage = new Vector();
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm"); 
		String folderName = sdf.format(new Date());
		
		File fileData = new File(rootPath+"sql\\"+folderName+".sql");
		FileWriter sqlWriter = null;
		try {			
			sqlWriter = new FileWriter(fileData,true);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}		
		
		try {
			File file = new File(rootPath+"pielove_smth.xml");
			config = new ScraperConfiguration(file);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		scraper = new Scraper(config, "output");
		System.out.println("all valid ID number = "+ vecAccountWithImage.size());
		int sqlIndex = 0;
		for (int x = 0; x < vecAccountWithImage.size(); x++) {
			Account account = (Account)vecAccountWithImage.elementAt(x);
			String authorId = account.getId();
			
			int articleId = new Integer(account.getArticleId()).intValue();

			System.out.println("articleId = "+ articleId);
			
			scraper.addVariableToContext("id", articleId);
			scraper.setDebug(true);

			scraper.execute();

			// takes variable created during execution
			Variable allcontent = (Variable)scraper.getContext().get("allcontent");
			Variable title = (Variable) scraper.getContext().get("title");
			//there are two possibilities: ends with -- or ends with ==
			Variable content = (Variable) scraper.getContext().get("content");// for ends with ==
//			Variable content2 = (Variable) scraper.getContext().get("content2"); //for ends with --
			Variable image = (Variable) scraper.getContext().get("image");
			if (title == null || title.toString().trim().length() == 0){
				System.out.println("title = "+ title);
				continue;
			}
				
			if (content == null || content.toString().trim().length() == 0) {
				/*if (content2 != null && content2.toString().trim().length() > 0) {
					content = content2;
				} else {*/
					System.out.println("content = " + content);
					continue;
				/*}*/
			}
			if (image == null || image.toString().trim().length() == 0){
				System.out.println("image = "+ image);
				continue;
			}

			int titleIndex = title.toString().indexOf("\\n");
			String titleResult = title.toString().substring(6, titleIndex);
			if (titleResult.startsWith("Re")){
				System.out.println("titleResult = "+ titleResult);
				continue;
			}
			titleResult = articleId+":"+titleResult;
			String[] imageArr = image.toString().split("attach");
			StringBuffer sbTargetImgUrl = new StringBuffer();
			
			
			
			for (int j = 1; j < imageArr.length; j++) {
				System.out.println("imageArr[j] = "+imageArr[j]);
				int firstExtIndex = imageArr[j].indexOf(".") + 1;
				int lastExtIndex = imageArr[j].lastIndexOf("'");
				String imageExt = imageArr[j].substring(firstExtIndex,
						lastExtIndex);
				int firstImageIndex = imageArr[j].lastIndexOf(",") + 2;
				
				String imageName = "";
				if (!imageArr[j].endsWith(";")){
					imageName = imageArr[j].substring(firstImageIndex);
				}
				else{
					int lastImageIndex = imageArr[j].lastIndexOf(")");
					imageName = imageArr[j].substring(firstImageIndex,
							lastImageIndex);
				}
				/*String imageName = "";
				int firstImageIndex = imageArr[j].lastIndexOf(",") + 2;
				if(imageArr[j].endsWith(";")){
					
				}else{
					
					imageName = imageArr[j].substring(firstImageIndex);
				}*/
				String imageNameResult = imageName + "." + imageExt;
				String imageURL = "http://att.newsmth.net/att.php?s.398." + articleId
						+ "." + imageNameResult;
				/*System.out.println("imageArr = " + imageNameResult);
				System.out.println("imageURL = " + imageURL);*/
				
				String targetName = new String();
				
				Image imageObj = new Image();
				imageObj.setSourceName(imageNameResult);
				imageObj.setUrl(imageURL);
				int beginIndex = imageURL.lastIndexOf(".");
				targetName = authorId+"_"+articleId+"_"+j+imageURL.substring(beginIndex);
				imageObj.setTargetName(targetName);
				
				vecImage.add(imageObj);
				String tempTargetUrl = "<img src=\"http://www.waitonewait.com/wp-content/uploads/image/"+targetName +"\"/>";
				sbTargetImgUrl.append(tempTargetUrl);
				
				
			}

			String contentStr = sbTargetImgUrl.toString()+ content.toString().replaceAll("\\\\n", "<br>");

			/*System.out.println("id = " + scraper.getContext().getVar("id"));
			System.out.println("title = " + titleResult);
			System.out.println("content = " + contentStr);*/
			sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
			String date = sdf.format(new Date());
			String titleUTF8 = new String();
			try {
				titleUTF8 = URLEncoder.encode(titleResult,"UTF-8");
			} catch (UnsupportedEncodingException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			String sql1 = "INSERT INTO `wp2_posts` (`ID`, `post_author`, `post_date`, `post_date_gmt`, `post_content`, `post_title`, `post_category`, `post_excerpt`, `post_status`, `comment_status`, `ping_status`, `post_password`, `post_name`, `to_ping`, `pinged`, `post_modified`, `post_modified_gmt`, `post_content_filtered`, `post_parent`, `guid`, `menu_order`, `post_type`, `post_mime_type`, `comment_count`) VALUES("+articleId+", 1, '"+date+"', '"+date+"', '"+contentStr+"', '"+titleResult+"', 0, '', 'publish', 'open', 'open', '', '"+titleUTF8+"', '', '', '"+date+"', '"+date+"', '', 0, 'http://www.waitonewait.com/?p="+articleId+"', 0, 'post', '', 0);\n";
			String sql2 = "INSERT INTO `wp2_term_relationships` (`object_id`, `term_taxonomy_id`, `term_order`) VALUES("+articleId+", 3, 0);\n";
			try {
				sqlWriter.write(sql1);
				sqlWriter.write(sql2);
				sqlIndex ++;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		try {
			sqlWriter.flush();
			sqlWriter.close();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		System.out.println("sql total number = "+sqlIndex);
		System.out.println("帖子解析结束，生成SQL文件："+rootPath+"sql\\"+folderName+".sql");
		System.out.println("开始下载图片...");

		try {
			File file = new File(rootPath+"downloadimage.xml");
			config = new ScraperConfiguration(file);
					//"D:/eclipseWS/WebHarvest/src/downloadimage.xml");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		

		scraper = new Scraper(config, rootPath+folderName);
		
		for(int i=0;i<vecImage.size();i++){
			Image image = (Image)vecImage.elementAt(i);
			String url = image.getUrl();
			scraper.addVariableToContext("imageurl", url);

			String targetName = image.getTargetName();
			scraper.addVariableToContext("imagename", targetName);
			scraper.setDebug(true);

			scraper.execute();
		}
		System.out.println("图片下载完成。");
		/*System.out.println("开始压缩图片...");
		Vector vecImageName = new Vector();
		for(int i=0;i<vecImage.size();i++){
			Image image = (Image)vecImage.elementAt(i);
			String targetName = image.getTargetName();
			vecImageName.add(targetName);
			ImageOperate.alterSize(rootPath+folderName+"\\"+targetName, 1024);
		}
		System.out.println("图片压缩完成。");*/
//		System.out.println("开始上传图片...");
//		FTPOperate.uploadFile("221.5.252.160", 21, "dandee", "iamsuper123", "/public_html/waitonewait271/wp-content/uploads/image", vecImageName, rootPath+folderName+"\\");
//		System.out.println("图片上传结束，共上传文件"+vecImageName.size()+"个");
		
	}

}